import { acceptHMRUpdate, defineStore } from 'pinia'
import {CommonGetUserList} from "~/requests";

const cacheKey = "__useUserStore"
export const useUserStore = defineStore('user', () => {
  const options = ref<SelectProps['options']>([]);

  const loadData = async () => {
    let response = await CommonGetUserList();
    options.value = response.data?.data.map((item) => {
      return {
        value: item['id'],
        label: item['label'],
      }
    })
    storeCache();
  }

  const storeCache = () => {
    if (options.value.length) {
      localStorage.setItem(cacheKey, JSON.stringify(options.value));
    }
  }
  const restoreCache = () => {
    const data = localStorage.getItem(cacheKey);
    if (data) {
      options.value = JSON.parse(data);
    }else {
      loadData();
    }
  }

  onMounted(() => {
    restoreCache();
  })

  return {
    options,
    loadData,
  }
})

if (import.meta.hot)
  import.meta.hot.accept(acceptHMRUpdate(useUserStore as any, import.meta.hot))
